package org.springframework.samples.mvc.basic.security;

import java.util.ArrayList;
import java.util.List;

import org.springframework.samples.mvc.basic.user.AuthenticationUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

public class SpringSecurityUserTransformerImpl implements SpringSecurityUserTransformer<AuthenticationUser> {

	@Override
	public UserDetails transform(AuthenticationUser user) {
		User u = new User(user.getUsername(), user.getPassword(), user.isEnabled(), !user.isAccountExpired(), !user.isCredentialsExpired(), !user.isAccountLocked(), transformAuthorities(user.getAuthorities()));
		return u;
	}
	
	private List<GrantedAuthority> transformAuthorities(List<String> authorities) {
		List<GrantedAuthority> ga = new ArrayList<GrantedAuthority>();
		for (String authority : authorities) {
			ga.add(new SimpleGrantedAuthority(authority));
		}
		return ga;
	}

}
